<template>
	<view>
		<view class="search-box">
			<u-search placeholder="工单单别" v-model="query.ub001" height="72" @custom="searchData" @search="searchData">
			</u-search>
			<br />
			<u-search placeholder="工单单号" v-model="query.ub002" height="72" @custom="searchData" @search="searchData">
			</u-search>
			<br />
			<u-search placeholder="日期" v-model="query.ub207" height="72" @focus="show = true" @custom="searchData"
				@search="searchData">
			</u-search>
		</view>
		<uni-table border stripe type="selection" emptyText="暂无更多数据" @selection-change="selectionChange">
			<uni-tr>
				<uni-th align="center">序号</uni-th>
				<uni-th align="center">工单单别</uni-th>
				<uni-th align="center">工单单号</uni-th>
				<uni-th align="center">生产顺序</uni-th>
				<uni-th align="center">批次号</uni-th>
				<uni-th align="center">工作中心</uni-th>
				<uni-th align="center">工艺编号</uni-th>
				<uni-th align="center">品号</uni-th>
				<uni-th align="center">投入数量</uni-th>
				<uni-th align="center">产出数量</uni-th>
				<uni-th align="center">辅单位</uni-th>
				<uni-th align="center">辅单位数量 </uni-th>
				<uni-th align="center">报废数量 </uni-th>
				<uni-th align="center">完工时间 </uni-th>
				<uni-th align="center">验收数量 </uni-th>
				<uni-th align="center">破坏数量 </uni-th>
				<uni-th align="center">报废数量 </uni-th>
			</uni-tr>
			<uni-tr v-for="(item,i) in  badCodeShowList" :key="i">
				<uni-td align="center">{{i+1}}</uni-td>
				<uni-td align="center">{{item.ub001}}</uni-td>
				<uni-td align="center">{{item.ub002}}</uni-td>
				<uni-td align="center">{{item.ub003}}</uni-td>
				<uni-td align="center">{{item.ub200}}</uni-td>
				<uni-td align="center">{{item.ub006}}</uni-td>
				<uni-td align="center">{{item.ub004}}</uni-td>
				<uni-td align="center">{{item.ta006}}</uni-td>
				<uni-td align="center">{{item.ub010}}</uni-td>
				<uni-td align="center">{{item.ub011}}</uni-td>
				<uni-td align="center">{{item.mb090}}</uni-td>
				<uni-td align="center">
					<input v-model="item.ub039" :disabled="!item.mb090" />
				</uni-td>
				<uni-td align="center">{{item.ub012}}</uni-td>
				<uni-td align="center">{{item.ub207}}</uni-td>
				<uni-td align="center">
					<input v-model="item.yssl" @input="handleInput(item,i)" />
				</uni-td>
				<uni-td align="center">
					<input v-model="item.phsl" @input="handleInput(item,i)" />
				</uni-td>
				<uni-td align="center">
					<input v-model="item.bfsl" @input="handleInput(item,i)" />
				</uni-td>
			</uni-tr>
		</uni-table>

		<view class="flowBefore-actions">
			<u-button class="buttom-btn" type="primary" @click.stop="handleConfirm">
				提交
			</u-button>
		</view>
		<u-calendar v-model="show" mode="range" @change="change"></u-calendar>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import request from "@/utils/request";
	export default {
		data() {
			return {
				badCodeShowList: [],
				indexList: [],
				show: false,
				tableLoad: false,
				splitType: false,
				btnLoading: false,
				dialogVisible: false,
				query: {
					ub001: undefined,
					ub002: undefined,
				},
				dataForm: {
					sfcubList: [],
				},
				multipleSelection: [],
				processOptions: [],
				listQuery: {
					currentPage: 1,
					pageSize: 20,
					sort: "desc",
					sidx: "",
				},
			}
		},
		onLoad(option) {

		},
		onShow() {
			this.init()
		},
		methods: {
			handleInput(row, i) {
				if (row.yssl > row.refyssl) {
					this.$refs.uToast.show({
						title: "验收数量不能大于" + row.refyssl,
						type: "error",
					});
					this.$nextTick(() => {
						this.badCodeShowList[i].yssl = row.refyssl;
					})
				} else if (row.bfsl > row.refbfsl) {
					this.$refs.uToast.show({
						title: "报废数量不能大于" + row.refbfsl,
						type: "error",
					});
					this.$nextTick(() => {
						this.badCodeShowList[i].bfsl = row.refbfsl;
					})
				} else if (row.phsl > row.refphsl) {

					this.$refs.uToast.show({
						title: "破坏数量不能大于" + row.refphsl,
						type: "error",
					});
					this.$nextTick(() => {
						this.badCodeShowList[i].phsl = row.refphsl;
					})

				}
			},
			change(e) {
				console.log(e);
				this.query.ub207 = `${e.startDate},${e.endDate}`
			},
			searchData() {
				this.init();
			},
			init() {
				let query = {
					...this.listQuery,
					...this.query,
				};
				request({
					url: "/api/SubDev/Sfcub/getFinishedSfcub",
					method: "get",
					data: query,
				}).then((res) => {
					this.badCodeShowList = res.data.list.map((item) => {
						return {
							...item,
							refyssl: item.yssl,
							refbfsl: item.bfsl,
							refphsl: item.phsl,
						};
					});
				})
			},
			// 多选
			selectionChange(e) {
				this.indexList = e.detail.index
				console.log(this.indexList)
			},
			//确 认
			handleConfirm() {
				const selectedObjects = this.indexList.map(index => this.badCodeShowList[index])
				console.log('selectedObjects', selectedObjects);

				request({
					url: "/api/SubDev/Sfcub/insertSfctb",
					method: "post",
					data: selectedObjects,
				}).then(res => {
					this.$refs.uToast.show({
						title: res.msg,
						type: "primary",
					});
					setTimeout(() => {
						uni.navigateBack()
					}, 300)

				})
			},
		}
	}
</script>
<style lang="scss">
	.search-box {
		height: 350rpx;
	}
</style>